package com.example.coffeeseller_2.widget;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * Created by Administrator on 2018/8/13.
 */

public class LineView extends View {
    private Paint mPaint;
    private int mDx;
    private LinearGradient mLinearGradient;
    private int left, top, right, bottom;
    private Resources r;

    public LineView(Context context) {
        super(context);
        init();
    }

    public LineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        r = getResources();
        init();
    }

    private void init() {
        mPaint = new Paint();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        this.left = left;
        this.right = right;
        this.top = top;
        this.bottom = bottom;
        ValueAnimator animator = ValueAnimator.ofInt(0, 2 * getMeasuredWidth());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mDx = (Integer) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setDuration(2000);
        animator.start();

        mLinearGradient = new LinearGradient(-getMeasuredWidth(), 0, 0, 0, new int[]{

        },
                new float[]{
                        0,
                        0.5f,
                        0xfffff6ff, 0xfffff6de, 0xfffff6ff
                },
                Shader.TileMode.CLAMP
        );
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
     /*   Matrix matrix = new Matrix();
        matrix.setTranslate(mDx, 0);
        mLinearGradient.setLocalMatrix(matrix);
        mPaint.setShader(mLinearGradient);
        mPaint.setStrokeWidth(20);
        mPaint.setColor(Color.parseColor("#dfa970"));
        canvas.drawLine(left, (top + bottom) / 2, right, (top + bottom) / 2, mPaint);*/
        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(this, "backgroundColor", 0xfffff6ff, 0xfffff6de, 0xfffff6ff);
        objectAnimator.setDuration(2000);
        //    objectAnimator.setEvaluator(new ArgbEvaluator());
        objectAnimator.setRepeatCount(-1);
        objectAnimator.start();
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                invalidate();
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                invalidate();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                invalidate();
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                invalidate();
            }
        });

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, r.getDisplayMetrics()); // 将dp转为px
        int minw = px;
        int w = resolveSizeAndState(minw, widthMeasureSpec, 0);
        int minh = px;
        int h = resolveSizeAndState(minh, heightMeasureSpec, 0);

        setMeasuredDimension(w, h);

    }
}
